const app = getApp()
var tcity = require("../../utils/city.js");
import Config from "../../common/config.js";
Page({
    data: {
        imgUrls: [],
        indicatorDots: true,
        // 联动选择器
        provinces: [],
        province: "",
        citys: [],
        city: "",
        countys: [],
        county: '',
        value: [0, 0, 0],
        values: [0, 0, 0],
        condition: false,
        product:[],
        joinImage:"",
        joinProcess:"",
        menu:[],
        phoneImage:"",
        imgbottom: "",
        clear:'',
        imgnum:2,
        namevalue:"",
        phonevalue: "",
        placevalue: "",
        selSta:1
    },
    toSubmit:function(e){
        console.log(e,"表单数据")
        var that = this
      if (that.data.namevalue == ''){
            wx.showModal({
                title: '姓名不能为空',
                content: '',
            })
            return;
      } else if (that.data.phonevalue == ''){
            wx.showModal({
                title: '手机号不能为空',
                content: '',
            })
            return;
      } else if (that.data.placevalue == '') {
            wx.showModal({
                title: '详细地址不能为空',
                content: '',
            })
            return;
        } else if (that.data.province == '请选择省') {
            wx.showModal({
                title: '请选择省份',
                content: '',
            })
            return;
        } else if (that.data.city == '请选择市') {
            wx.showModal({
                title: '请选择城市',
                content: '',
            })
            return;
        } else if (that.data.county == '请选择区') {
            wx.showModal({
                title: '请选择县区',
                content: '',
            })
            return;
        }
        var sessionId = wx.getStorageSync('sessionId')
        wx.request({
          url: Config.url + '/api/consigneeInfo/add_consigneeInfo',
            method:'POST',
            header: {
              'content-type': 'application/json', // 默认值
                'cookie': sessionId
            },
            data:{
              consigneeName: that.data.namevalue,
              consigneeMobile: that.data.phonevalue,
              consigneeAddress: that.data.placevalue,
                province: that.data.province,
                city: that.data.city,
                area:that.data.county,
                isDefault: that.data.selSta
            },
            success:function(res){
              console.log(res)
                if (res.data.code==100){
                    wx.showModal({
                        title: '提交成功！',
                        content: '',
                    })
                  wx.redirectTo({
                    url: '../adressList/adressList'
                  });//返回上一页
                    that.setData({
                        clear:''
                    })
                }else{
                    wx.showModal({
                        title: '提交失败！',
                        content: '',
                    })
                }

            },
            fail:function(){
                wx.showModal({
                    title: '提交失败！',
                    content: '',
                })
            }
        })
        console.log(e, "表单数据", this.data.city)
    },
    goTo:function(e){
        var id = e.currentTarget.dataset.id
        console.log(e.currentTarget.dataset.id)
        wx.navigateTo({
            url: '../article/article?id=' + id,
        })
    },
    //幻灯片
    changeIndicatorDots: function (e) {
        this.setData({
            indicatorDots: !this.data.indicatorDots
        })
    },
    changeAutoplay: function (e) {
        this.setData({
            autoplay: !this.data.autoplay
        })
    },
    intervalChange: function (e) {
        this.setData({
            interval: e.detail.value
        })
    },
    durationChange: function (e) {
        this.setData({
            duration: e.detail.value
        })
    },

//联动选择器
    bindChange: function (e) {
        console.log(e);
        var val = e.detail.value
        var t = this.data.values;
        var cityData = this.data.cityData;

        if (val[0] != t[0]) {
            console.log('province no ');
            const citys = [];
            const countys = [];

            for (let i = 0; i < cityData[val[0]].sub.length; i++) {
                citys.push(cityData[val[0]].sub[i].name)
            }
            for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
                countys.push(cityData[val[0]].sub[0].sub[i].name)
            }

            this.setData({
                province: this.data.provinces[val[0]],
                city: cityData[val[0]].sub[0].name,
                citys: citys,
                county: cityData[val[0]].sub[0].sub[0].name,
                countys: countys,
                values: val,
                value: [val[0], 0, 0]
            })

            return;
        }
        if (val[1] != t[1]) {
            console.log('city no');
            const countys = [];

            for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
                countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
            }

            this.setData({
                city: this.data.citys[val[1]],
                county: cityData[val[0]].sub[val[1]].sub[0].name,
                countys: countys,
                values: val,
                value: [val[0], val[1], 0]
            })
            return;
        }
        if (val[2] != t[2]) {
            console.log('county no');
            this.setData({
                county: this.data.countys[val[2]],
                values: val
            })
            return;
        }
    },
    open: function () {
        this.setData({
            condition: !this.data.condition
        })
    },
    showView:function(){
        this.setData({
            condition: true
        })
    },

    ccityInit:function(){
        var that = this
        tcity.init(this);

        var cityData = that.data.cityData;


        const provinces = [];
        const citys = [];
        const countys = [];

        for (let i = 0; i < cityData.length; i++) {
            provinces.push(cityData[i].name);
        }
        console.log('省份完成');
        for (let i = 0; i < cityData[0].sub.length; i++) {
            citys.push(cityData[0].sub[i].name)
        }
        console.log('city完成');
        for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
            countys.push(cityData[0].sub[0].sub[i].name)
        }

        that.setData({
            'provinces': provinces,
            'citys': citys,
            'countys': countys,
            'province': cityData[0].name,
            'city': cityData[0].sub[0].name,
            'county': cityData[0].sub[0].sub[0].name
        })
        console.log(provinces)
        console.log('初始化完成');
    },
  bindName: function (e) {
    var namevalue = e.detail.value
    console.log(namevalue)
    this.setData({
      namevalue: namevalue
    })
  },
  bindPhone: function (e) {
    var phonevalue = e.detail.value
    this.setData({
      phonevalue: phonevalue
    })
  },
  bindPlace: function (e) {
    var placevalue = e.detail.value
    this.setData({
      placevalue: placevalue
    })
    console.log(this.data.placevalue)
  },
  selAdree:function() {
    var that = this
    if (that.data.selSta==1){
      that.setData({
        selSta:2
      })
    } else if (that.data.selSta == 2){
      that.setData({
        selSta: 1
      })
    }
  },
    onLoad: function () {
        console.log("onLoad");
        var that = this;
        this.ccityInit()
    }
})
